﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;

namespace Core
{
    public partial class ParamBase
    {

        public virtual string Table { get; set; }

        private List<Field> _fieldAlls = new List<Field>();
        public List<Field> FieldAlls {
            get {
                /*var allFields = new Field[FieldInserts.Count + WhereFields.Count + FieldUpdates.Count];
                FieldInserts.CopyTo(allFields, 0);
                WhereFields.CopyTo(allFields, FieldInserts.Count);
                FieldUpdates.CopyTo(allFields, FieldInserts.Count + WhereFields.Count);
                return allFields.ToList();
                 */

                if (_fieldAlls == null) _fieldAlls = new List<Field>();
                return _fieldAlls;
            }
            set { _fieldAlls = value; }

        }

        private List<Field> _fieldInserts = new List<Field>();
        public List<Field> FieldInserts {
            get {
                if (_fieldInserts == null) _fieldInserts = new List<Field>();
                return _fieldInserts;
            }
            set { _fieldInserts = value; }
        }


        private List<Field> _fieldWheres = new List<Field>();
        public List<Field> FieldWheres {
            get { return _fieldWheres; }
            set { _fieldWheres = value; }
        }


        private List<Field> _fieldUpdates = new List<Field>();
        public List<Field> FieldUpdates {
            get { return _fieldUpdates; }
            set { _fieldUpdates = value; }
        }

        public ParamBase AddFieldUpdate(string paramName, string value) {
            this.FieldUpdates.Add(new Field { Name = paramName, Value = value });
            return this;
        }

        public ParamBase SetFieldValue(string name, string value) {
            var field = this.FieldAlls.FirstOrDefault(x => x.Name == name);
            field.Value = value;
            return this;
        }
        public string GetFieldValue(string name) {
            var field = this.FieldAlls.FirstOrDefault(x => x.Name == name);
            return field.Value;
        }



        public static ParamBase Instance() {
            return new ParamBase();
        }

        public ParamBase AddWhere(string paramName, string value) {
            _fieldWheres.Add(new Field { Name = paramName, Value = value });
            return this;
        }

        public OperateType Operator { get; set; }
    }

    public enum OperateType
    {
        Select,
        Insert,
        Update,
        Delete
    }
}
